<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'" destroy-on-close :close-on-click-modal="false"
        :visible.sync="visible" :before-close="closeDailog">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="80px">

            <ec-form-item-header value="组织基本信息"></ec-form-item-header>

            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="所属集团" prop="pkGroup">
                        <ec-group-tree-select v-model="dataForm.pkGroup" placeholder="所属集团">
                        </ec-group-tree-select>
                    </el-form-item>

                </el-col>
                <el-col :span="12">
                    <el-form-item label="上级组织" prop="pkFatherorg">
                        <ec-org-tree-select :pkGroup="dataForm.pkGroup" v-model="dataForm.pkFatherorg"
                            placeholder="上级组织">
                        </ec-org-tree-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="组织名称" prop="name">
                        <el-input clearable v-model="dataForm.name" placeholder="组织名称"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="简称" prop="shortname">
                        <el-input clearable v-model="dataForm.shortname" placeholder="简称"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="负责人" prop="principal">
                        <!-- <el-input clearable v-model="dataForm.principal" placeholder="负责人"></el-input> -->
                        <!-- <ec-select v-if="visible" v-model="dataForm.principal" :pkGroup="dataForm.pkGroup" :pkOrg="dataForm.pkOrg" placeholder="负责人" source="business">
                        </ec-select> -->

                        <ec-user-select v-if="visible" v-model="dataForm.principal" :pkGroup="dataForm.pkGroup"
                            :pkOrg="dataForm.pkOrg" placeholder="负责人" @getOptionDetail="getPrincipal"></ec-user-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="电话" prop="tel">
                        <el-input clearable v-model.number="dataForm.tel" placeholder="电话"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="15">
                <el-col :span="12"></el-col>
                <el-col :span="12"></el-col>
            </el-row>
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="内部编码" prop="innercode">
                        <el-input clearable v-model="dataForm.innercode" placeholder="内部编码"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="启用状态" prop="enablestate">
                        <ec-dict-select v-if="visible" v-model="dataForm.enablestate" placeholder="请选择启用状态"
                            dictType="enable_state">
                        </ec-dict-select>
                    </el-form-item>
                </el-col>
            </el-row>

        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as orgService from '@/api/upms/org'
    import * as groupService from '@/api/upms/group'
    export default {
        data() {
            var checkPhone = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('手机号不能为空'));
                } else {
                    const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
                    if (reg.test(value)) {
                        callback();
                    } else {
                        return callback(new Error('请输入正确的手机号'));
                    }
                }
            }
            return {
                visible: false,
                submitLoading: false,
                orgsTree: [],
                groupTree: [],
                dataForm: {
                    pkOrg: 0,
                    pkGroup: '',
                    pkFatherorg: '',
                    principal: '',
                    name: '',
                    shortname: '',
                    tel: '',
                    displayorder: '',
                    enablestate: 'Y',
                    innercode: '',
                    dataoriginflag: '',
                    revision: '',
                },
                dataRule: {
                    pkGroup: [{
                        required: true,
                        message: '所属集团不能为空',
                        trigger: 'change'
                    }],
                    pkFatherorg: [{
                        required: false,
                        message: '上级组织不能为空',
                        trigger: 'change'
                    }],
                    principal: [{
                        required: false,
                        message: '负责人不能为空',
                        trigger: 'change'
                    }],
                    shortname: [{
                        required: false,
                        message: '简称不能为空',
                        trigger: 'blur'
                    }],
                    name: [{
                        required: true,
                        message: '组织名称不能为空',
                        trigger: 'blur'
                    }],
                    // tel: [{
                    //     required: false,
                    //     validator: checkPhone,
                    //     trigger: 'blur'
                    // }],
                    enablestate: [{
                        required: true,
                        message: '启用状态不能为空',
                        trigger: 'change'
                    }],
                    innercode: [{
                        required: true,
                        message: '内部编码不能为空',
                        trigger: 'blur'
                    }],
                },
            }
        },
        methods: {
            init(id) {
                this.dataForm.pkOrg = id || 0
                this.visible = true
                let that = this
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.pkOrg) {
                        orgService.getorginfo(this.dataForm.pkOrg).then(data => {
                            this.dataForm.pkGroup = data.pkGroup
                            this.dataForm.pkFatherorg = data.pkFatherorg
                            this.dataForm.principal = data.principal
                            this.dataForm.shortname = data.shortname
                            this.dataForm.name = data.name
                            this.dataForm.tel = data.tel
                            this.dataForm.displayorder = data.displayorder
                            this.dataForm.enablestate = data.enablestate
                            this.dataForm.innercode = data.innercode
                            this.dataForm.dataoriginflag = data.dataoriginflag
                            this.dataForm.revision = data.revision
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'pkOrg': this.dataForm.pkOrg || undefined,
                            'pkGroup': this.dataForm.pkGroup,
                            'pkFatherorg': this.dataForm.pkFatherorg,
                            'principal': this.dataForm.principal,
                            'shortname': this.dataForm.shortname,
                            'name': this.dataForm.name,
                            'tel': this.dataForm.tel,
                            'displayorder': this.dataForm.displayorder,
                            'enablestate': this.dataForm.enablestate,
                            'innercode': this.dataForm.innercode,
                            'dataoriginflag': this.dataForm.dataoriginflag,
                            'def1': this.dataForm.def1,
                            'def2': this.dataForm.def2,
                            'def3': this.dataForm.def3,
                            'def4': this.dataForm.def4,
                            'def5': this.dataForm.def5,
                            'revision': this.dataForm.revision,
                            'createdBy': this.dataForm.createdBy,
                            'createdTime': this.dataForm.createdTime,
                            'updatedBy': this.dataForm.updatedBy,
                            'updatedTime': this.dataForm.updatedTime
                        }
                        var tick = !this.dataForm.pkOrg ? orgService.addorg(params) : orgService
                            .updateorg(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
            // 自动获取手机号
            getPrincipal(info){
                if (info && this.$isEmpty(this.dataForm.tel)){
                    this.dataForm.tel = info.phone
                }
            }
        }
    }
</script>